package org.classg.vod.controller;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.classg.servicebase.exception.MyException;
import org.classg.utils.R;
import org.classg.vod.service.VideoService;
import org.classg.vod.utils.AliyunVodSDKUtils;
import org.classg.vod.utils.ConstantPropertiesUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@RequestMapping("/vodservice/video")
@Api(description = "阿里云视频点播微服务")
@CrossOrigin
public class VideoController {

    @Autowired
    private VideoService videoService;

    @ApiOperation("阿里云视频上传")
    @PostMapping("/uploadVideo")
    public R uploadVideo(
            @ApiParam(name = "file", value = "视频文件", required = true)
            @RequestParam("file") MultipartFile file) {
        String videoId = videoService.uploadVideo(file);
        return R.ok().message("视频上传成功！").data("videoId", videoId);
    }

    @ApiOperation("根据视频id删除阿里云视频")
    @DeleteMapping("/deleteVideoByVideoId/{videoId}")
    public R deleteVideoByVideoId(
            @ApiParam(name = "videoId", value = "视频id", required = true)
            @PathVariable("videoId") String videoId) {
        videoService.deleteVideoByVideoId(videoId);
        return R.ok().message("根据视频id删除阿里云视频成功！");
    }

    @ApiOperation("根据多个视频id删除阿里云多个视频")
    @DeleteMapping("/deleteVideoByVideoIds/{videoIds}")
    public R deleteVideoByVideoIds(
            @ApiParam(name = "videoIds", value = "多个视频id", required = true)
            @RequestParam("videoIds") List<String> videoIds) {
        videoService.deleteVideoByVideoIds(videoIds);
        return R.ok().message("根据多个视频id删除阿里云多个视频成功！");
    }

    @ApiOperation("根据视频id获取播放凭证")
    @GetMapping("/queryPlayAuthByVideoId/{videoId}")
    public R queryPlayAuthByVideoId(@ApiParam(name = "videoId", value = "视频id", required = true) @PathVariable String videoId) {
        DefaultAcsClient client = null;
        GetVideoPlayAuthRequest request = null;
        GetVideoPlayAuthResponse response = null;
        String playAuth = null;
        try {
            // 初始化对象
            client = AliyunVodSDKUtils.initVodClient(ConstantPropertiesUtil.ACCESS_KEY_ID, ConstantPropertiesUtil.ACCESS_KEY_SECRET);
            request = new GetVideoPlayAuthRequest();
            request.setVideoId(videoId);
            response = client.getAcsResponse(request);
            playAuth = response.getPlayAuth();
        } catch (ClientException e) {
            e.printStackTrace();
            throw new MyException(20001, "获取阿里云视频凭证失败！");
        }
        return R.ok().data("playAuth", playAuth).message("获取凭证成功！");
    }

}

